package com.ruoyi.car.controller.admin.store;

import com.github.pagehelper.PageInfo;
import com.ruoyi.car.controller.admin.model.vo.CarModelAddReq;
import com.ruoyi.car.controller.admin.model.vo.CarModelEditReq;
import com.ruoyi.car.controller.admin.model.vo.CarModelResp;
import com.ruoyi.car.controller.admin.store.vo.CarStoreAddReq;
import com.ruoyi.car.controller.admin.store.vo.CarStoreEditReq;
import com.ruoyi.car.controller.admin.store.vo.CarStoreResp;
import com.ruoyi.car.entity.CarModel;
import com.ruoyi.car.entity.CarStore;
import com.ruoyi.car.mapstruct.CarModelConvert;
import com.ruoyi.car.mapstruct.CarStoreTo;
import com.ruoyi.car.service.CarModelService;
import com.ruoyi.car.service.CarStoreService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.PageReq;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.MessageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author：流冬
 * @Package：com.ruoyi.car.controller.admin.store
 * @Project：RuoYi-Vue
 * @name：StoreController
 * @Date：2023/6/5 20:48
 * @Filename：StoreController
 * @Description
 */
@Api(tags = "管理后台-仓库信息")
// springmvc的 @Controller+@ResponseBody
@RestController
// springmvc注解
@RequestMapping(value = "/car/store")
public class StoreController {
    @Autowired
    private CarStoreService carStoreService;


    // spring security权限
    @PreAuthorize(value = "@ss.hasPermi('car:store:page')")
    // springmvc  get请求注解
    @GetMapping(value = "/page")
    // swagger3 的注解
    // @Operation(summary = "获取分页列表")
    @ApiOperation(value = "获取分页列表")
    public TableDataInfo list(PageReq req){
        PageInfo<CarStore> pageInfo = this.carStoreService.search(req);

        //总记录数
        long count= pageInfo.getTotal();
        //转换完成以后的结果
        List<CarStoreResp> list= CarStoreTo.INSTANCE.convert(pageInfo.getList());

        TableDataInfo tableDataInfo=new TableDataInfo(list,count);
        return tableDataInfo;

    }

    @Log(title = "仓库管理",businessType = BusinessType.INSERT)
    @PreAuthorize(value = "@ss.hasPermi('car:store:add')")
    @ApiOperation(value = "新增")
    @PostMapping
    public R add(@RequestBody @Valid CarStoreAddReq req){
        CarStore convert = CarStoreTo.INSTANCE.convert(req);
        boolean save = this.carStoreService.save(convert);
        return save? R.ok(): R.fail();
    }

    @PreAuthorize(value = "@ss.hasPermi('car:store:query')")
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = "/{id}")
    public R<CarStoreResp> findById(@PathVariable(value = "id") Long id){
        CarStore db = this.carStoreService.getById(id);
        if(db==null){
            //国际化 i18n 中查询
            String msg= MessageUtils.message("db.not.found");
            throw new ServiceException(msg);
        }

        CarStoreResp convert = CarStoreTo.INSTANCE.convert(db);
        return R.ok(convert);
    }

    @Log(title = "仓库管理",businessType = BusinessType.UPDATE)
    @PreAuthorize(value = "@ss.hasPermi('car:store:edit')")
    @ApiOperation(value = "修改保存")
    @PutMapping
    public R  update(@RequestBody @Valid CarStoreEditReq req){
        CarStore db = this.carStoreService.getById(req.getId());
        if(db==null){
            //国际化 i18n 中查询
            String msg=MessageUtils.message("db.not.found");
            throw new ServiceException(msg);
        }

        CarStore convert = CarStoreTo.INSTANCE.convert(req);

        boolean save = this.carStoreService.updateById(convert);
        return save? R.ok(): R.fail();
    }
}
